package org.linlinjava.litemall.admin.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ObjectArrayCodec;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.admin.util.StatVo;
import org.linlinjava.litemall.admin.vo.Json;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallWalletRecord;
import org.linlinjava.litemall.db.domain.LitemallWalletRecordVo;
import org.linlinjava.litemall.db.service.StatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin/stat")
public class AdminStatController {
    private static final Logger log = LoggerFactory.getLogger(AdminStatController.class);

    @Autowired
    private StatService statService;

    @GetMapping("/user")
    public Object statUser(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        List<Map> rows = statService.statUser();
        String[] columns = new String[]{"day", "users"};
        StatVo statVo = new StatVo();
        statVo.setColumns(columns);
        statVo.setRows(rows);

        return ResponseUtil.ok(statVo);
    }

    @GetMapping("/order")
    public Object statOrder(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        List<Map> rows = statService.statOrder();
        String[] columns = new String[]{"day", "orders", "customers", "amount", "integral"};
        StatVo statVo = new StatVo();
        statVo.setColumns(columns);
        statVo.setRows(rows);

        return ResponseUtil.ok(statVo);
    }

    @GetMapping("/integral")
    public Object statIntegral(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        List<Map> rows = statService.statIntegral();
        String[] columns = new String[]{"name", "integral_amount_all", "integral_amount_used"};
        StatVo statVo = new StatVo();
        statVo.setColumns(columns);
        statVo.setRows(rows);


        return ResponseUtil.ok(statVo);
    }
    @GetMapping("/goods")
    public Object statGoods(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        List<Map> rows = statService.statGoods();
        String[] columns = new String[]{"day", "orders", "products", "amount"};
        StatVo statVo = new StatVo();
        statVo.setColumns(columns);
        statVo.setRows(rows);


        return ResponseUtil.ok(statVo);
    }

    @PostMapping("/report")
    public Object report(@RequestBody String body) {
        String oper = "report";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        Byte type = json.getByte("type");
        String nickname = json.getString("nickname");
        String name = json.getString("name");
        Integer payType = json.getInteger("payType");
        Integer mechanismId = json.getInteger("mechanismId");
        Integer userId = json.getInteger("userId");
        Date start = null;
        Date end = null;
        Date payStart = null;
        Date payEnd = null;
        Object timeInterval = json.get("timeInterval");
        Object timeIntervalOrganization = json.get("timeIntervalOrganization");
        String sort = json.getString("sort");
        String order = json.getString("order");
        String phone = json.getString("phone");
        int page = json.getIntValue("page");
        int limit = json.getIntValue("limit");

//        if (adminId == null) {
        ////            return ResponseUtil.unlogin();
        ////        }

        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }
        if (timeIntervalOrganization != null && timeIntervalOrganization.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeIntervalOrganization;
            payStart = timeIntervalarr.getTimestamp(0);
            payEnd = timeIntervalarr.getTimestamp(1);
        }
        List<LitemallWalletRecordVo> report = statService.queryInstitutionalReportVo(start, end, type, nickname, name, payStart, payEnd,payType, mechanismId, userId, page, limit, sort, order,phone);
        long total = PageInfo.of(report).getTotal();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", report);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/export")
    public Object export(@RequestBody String body) {
        String oper = "report";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        Byte type = json.getByte("type");
        String nickname = json.getString("nickname");
        String name = json.getString("name");
        Date start = null;
        Date end = null;
        Date payStart = null;
        Date payEnd = null;
        Integer payType = json.getInteger("payType");
        Integer mechanismId = json.getInteger("mechanismId");
        String phone = json.getString("phone");
        Integer userId = json.getInteger("userId");
        Object timeInterval = json.get("timeInterval");
        Object timeIntervalOrganization = json.get("timeIntervalOrganization");
        String sort = json.getString("sort");
        String order = json.getString("order");

        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }
        if (timeIntervalOrganization != null && timeIntervalOrganization.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeIntervalOrganization;
            payStart = timeIntervalarr.getTimestamp(0);
            payEnd = timeIntervalarr.getTimestamp(1);
        }
        List<LitemallWalletRecordVo> report = statService.queryTotalInstitutionalReportVo(start, end, type, nickname, name, payStart, payEnd,payType, mechanismId, userId, sort, order,phone);
        Map<String, Object> data = new HashMap<>();
        data.put("items", report);
        return ResponseUtil.ok(data);
    }
}
